package vip.websky.project.web.shiro;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import vip.websky.project.common.utils.DateUtils;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.module.bo.user.SysUserBO;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.response.user.SysUserRespDTO;
import vip.websky.project.user.server.UserServer;
import vip.websky.project.user.service.UserService;
import vip.websky.project.web.controller.BaseController;

/**
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/10/10-15:20
 * <p>
 * ShiroRealm 权限认证
 **/
@Slf4j
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserServer userServer;

    public ShiroRealm(CacheManager cacheManager) {
        super(cacheManager);
    }

    /**
     * Shiro登录认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        log.info("Shiro开始登录认证");
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        //1.参数转换
        SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(token, SysUserBO.class);
        sysUserBO=userServer.selectUser(sysUserBO);
        if (sysUserBO!=null) {
            if (sysUserBO.equals(sysUserBO.getUserStatus())) {
                throw new DisabledAccountException("帐号已经禁止登录！");
            }
        } else {
            throw new UnknownAccountException("账户不存在！");
        }
        Session currentSession = SecurityUtils.getSubject().getSession();
        BaseController.loginIn(currentSession,sysUserBO);
        return new SimpleAuthenticationInfo(sysUserBO, sysUserBO.getPassword(), getName());
    }

    /**
     * Shiro权限认证
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        log.info("Shiro开始权限认证");
        SysUserBO sysUserBO = (SysUserBO) principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        /*info.addRoles(sysUserBO.getRoleIds());*/
        return info;
    }

    @Override
    public void onLogout(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
        SysUserBO shiroUser = (SysUserBO) principals.getPrimaryPrincipal();
        removeUserCache(shiroUser);
    }

    /**
     * 清除用户缓存
     *
     * @param shiroUser
     */
    public void removeUserCache(SysUserBO shiroUser) {
        removeUserCache(shiroUser.getUserName());
    }

    /**
     * 清除用户缓存
     *
     * @param loginName
     */
    public void removeUserCache(String loginName) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection();
        principals.add(loginName, super.getName());
        super.clearCachedAuthenticationInfo(principals);
    }
}
